﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;

/// <summary>
/// Summary description for ChuongTrinh
/// </summary>
public class ChuongTrinh:DBHelper
{
	public ChuongTrinh()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public IEnumerable NewPrograms()
    {
        //IQueryable<chuongtrinh>
        //var db = new LanAnhDataContext(ConnectionString);
        //var chuongtrinh = (from ct in db.chuongtrinhs
        //                  orderby ct.mact descending
        //                  select ct).Take(5);
        //return chuongtrinh;
        using (var db = new LanAnhDataContext(ConnectionString))
        {

            if (check == 1)
            {
                return db.sp_XemChuongTrinh_Error_DirtyRead().Take(5).ToList();
            }
            else
            {
                return db.sp_XemChuongTrinh_Fix_DirtyRead().Take(5).ToList();
            }
         
        }
    }
    public IEnumerable GetData()
    {
        //var db = new LanAnhDataContext(ConnectionString);
        //var chuongtrinh = from ct in db.chuongtrinhs                         
        //                  select ct;
        //return chuongtrinh;
        //sp_XemChuongTrinh_Error_DirtyRead or sp_XemChuongTrinh_Fix_DirtyRead
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            if (check == 1)
            {
                return db.sp_XemChuongTrinh_Error_DirtyRead().ToList();
            }
            else
            {
                return db.sp_XemChuongTrinh_Fix_DirtyRead().ToList();
            }
        }
    }
   

    public IQueryable<chuongtrinh> GetData(int id)
    {
        var db = new LanAnhDataContext(ConnectionString);
        var chuongtrinh = from ct in db.chuongtrinhs
                          where ct.mact == id
                          select ct;
        return chuongtrinh;

    }

    public static IEnumerable GetAll(int start = 0, int take = 5)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var chuongtrinh = db.chuongtrinhs.Select(c => new
            {
                ct = c,
                tenLoaiCt = db.loaichuongtrinhs.Single(lc => lc.maloaict == c.maloaict).tenloaict
            }).OrderByDescending(c => c.ct.mact).Skip(start).Take(take);

            return chuongtrinh.ToList();
        }
    }
    public static IEnumerable GetAll()
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var chuongtrinh = db.chuongtrinhs.Select(c => new
            {
                ct = c,
                tenLoaiCt = db.loaichuongtrinhs.Single(lc => lc.maloaict == c.maloaict).tenloaict
            }).OrderByDescending(c => c.ct.mact);

            return chuongtrinh.ToList();
        }
    }


    public static void UpdateChuongTrinh(int mact, string tenct, int thoiluong, string mota, string hinhanh)
    {
        using(var db = new LanAnhDataContext(ConnectionString))
        {
            //do bi dalay nen qua trinh luu du lieu se tien hanh hoi lau mot chut
            if (check == 1)
            {
                db.sp_EditChuongTrinh_Error_Deadlock(mact,tenct,thoiluong,mota,hinhanh);
            }
            else
            {
                db.sp_EditChuongTrinh_Fix_Deadlock(mact, tenct, thoiluong, mota, hinhanh);
            }
        }
    }
    public static void DeleteChuongtrinh(int mact)
    {
        var db = new LanAnhDataContext(ConnectionString);
        chuongtrinh ct = db.chuongtrinhs.Single(x => x.mact == mact);
        db.chuongtrinhs.DeleteOnSubmit(ct);
        db.SubmitChanges();
    }
    public static IEnumerable GetDataLoaiChuongTrinh()
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            if (check == 1)
            {
                return db.sp_LayDanhSachLoaiChuongTrinh_Error_DirtyRead().ToList();
            }
            else
            {
                return db.sp_LayDanhSachLoaiChuongTrinh_Fix_DirtyRead().ToList();
            }
        }
    }
    public static void InsertLoaiChuongTrinh(string tenloai)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            db.sp_ThemLoaiChuongTrinh_DirtyRead(tenloai);
        }
    }

    public static List<chuongtrinh> ChuongTrinhList()
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var chuongtrinh = from ct in db.chuongtrinhs
                              select ct;
            return chuongtrinh.ToList();
        }
    }
    public static IEnumerable GetLoaiCT()
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var loaiCT = db.loaichuongtrinhs.OrderByDescending(l => l.maloaict);
            return loaiCT.ToList();
        }
    }

    public static IEnumerable GetTenCT()
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var CT = db.chuongtrinhs.OrderByDescending(l => l.mact);
            return CT.ToList();
        }
    }

    public static IEnumerable GetCtByLoaiCt(int maloaict)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var cts = db.chuongtrinhs.Where(
                    ct => ct.maloaict == maloaict).Select(
                        ct => new
                        {
                            ct
                        });
            return cts.ToList();
        }
    }

    public static string NullSafeToString(object obj)
    {
        return obj != null ? obj.ToString() : "0";
    }

    public static void ThemChuongTrinh(string tenct, int thoiluong, string mota, int maloaict, string hinhanh)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            if (themct == 1)
                db.sp_ThemChuongTrinh_Phantom(tenct, thoiluong, mota, maloaict, hinhanh);
            else
                db.sp_ThemChuongTrinh_DirtyRead(tenct, thoiluong, mota, maloaict, hinhanh);
        }

    }

    public static void CapNhatChuongTrinh(int mact, int thoiluong)
    {
        using(var db = new LanAnhDataContext(ConnectionString))
        {
            if (check == 1)
                db.sp_CapNhatChuongTrinh_Error_LostUpdate_Transaction1(mact, thoiluong);
            else
                db.sp_CapNhatChuongTrinh_Fix_LostUpdate_Transaction1(mact, thoiluong);
        }
    }

    public static IEnumerable XemChuongTrinh_PhanTom(int maloaict, ref int total)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            IEnumerable ctrinh = null;
            Nullable<int> result = null;
            if (check == 1)
                ctrinh = db.sp_XemChuongTrinh_Error_Phantom(maloaict, ref result).ToArray();
            else
                ctrinh = db.sp_XemChuongTrinh_Fix_Phantom(maloaict, ref result).ToArray();
            total = Convert.ToInt32(result);
            return ctrinh;
        }
    }

    public static IEnumerable LastChuongTrinh(int take = 5)
    {
        using(var db = new LanAnhDataContext(ConnectionString))
        {
            return db.chuongtrinhs.OrderByDescending(c => c.mact).Take(take).ToList();
        }
    }
}

    


